<template>
    <el-dialog class="edit-password" title="修改密码" append-to-body :close-on-click-modal="closeOnclickModal"
               width="500px" top="3.8vh" :visible.sync="isDialogShow" @close="close">
        <el-form :model="dialogForm" ref="dialogForm" :size="size" label-width="100px">
            <el-form-item :rules="yh_rules({required:true, min: 6})" label="新密码" class="password_item" prop="password">
                <el-input placeholder="请输入密码" v-model="dialogForm.password"
                          maxlength="18"
                          show-password></el-input>
            </el-form-item>
            <el-form-item :rules="yh_rules({required:true})" label="确认密码" prop="rePassword">
                <el-input placeholder="请输入密码" v-model="dialogForm.rePassword" show-password></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close" :size="size">取消</el-button>
            <el-button type="primary" @click="submit" :size="size">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
  import {updatePassword} from '@/api/user'
  import { mapGetters } from 'vuex'
  export default {
    name: 'DialogUpdatePassword',
    props: {
      updatePasswordDialogShow: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        dialogForm: {
          password: '',
          rePassword: ''
        },
        isDialogShow: false
      }
    },
    computed: {
      ...mapGetters(['login_user'])
    },
    watch: {
      updatePasswordDialogShow (newValue) {
        this.isDialogShow = newValue
      }
    },
    methods: {
      validatePassWord() {
        let {password, rePassword} = this.registerForm
        if (password !== rePassword) {
          this.$message.warning('两次输入密码不同')
        }
        return password === rePassword
      },
      submit() {
        this.$refs.registerForm.validate(valid => {
          if (valid) {
            if (!this.validatePassWord()) return
            this.updatePassword()
          }
        })
      },
      updatePassword() {
        updatePassword(this.dialogForm).then(res => {
          this.$message.success('修改成功')
        })
      },
      close () {
        this.dialogForm = {
          password: '',
          rePassword: ''
        }
        this.$emit('update:updatePasswordDialogShow', false)
      },
    }
  }
</script>

<style lang="scss">
    .password_item {
        position: relative;
    }
</style>
